<?php
namespace app\index\controller\user;

use app\index\controller\Base;

class user extends Base
{
    public $tableName = "Admin";
    public function index()
    {
        $this->page([],10,'','id desc','role');
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('Admin');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
           
            $data["username"] = $post["username"];
            $data["userpwd"] =md5($post["userpwd"]);    
            $data["nickname"] = $post["nickname"];    
            $data["role_id"] = $post["role_id"];
            $AdminModel = model("Admin");
            $result = $AdminModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");
            }
            $this->success("操作成功", url("/Index/User/user/index"));
        }
       $data["role"]= model("Role")->where(["status"=>0])->select();
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $AdminModel = model('Admin');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();           
            $data["username"] = $post["username"];
            if(!empty($post["userpwd"])){
                $data["userpwd"] =md5($post["userpwd"]);
            }          
            $data["role_id"] = $post["role_id"];
            $data["nickname"] = $post["nickname"];
            
            $map["id"] = $post["id"];
            $result = $AdminModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/User/user/index"));
        }

        $id = input("id");

        $data["info"] = $AdminModel->get($id);
        $data["role"]= model("Role")->where(["status"=>0])->select();
      
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $AdminModel = model('Admin');
        $result = $AdminModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
